package com.accesshollywood.grfxOrdering.panels;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;

import org.joda.time.DateTime;

import com.accesshollywood.grfxOrdering.DateTimePreset;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;
import com.lavantech.gui.comp.DateTimePicker;

public class DueDatePanel extends JPanel {
	public DateTimePicker	dateTimePicker;
	public JComboBox		comboBoxQuickDateTime;

	public DueDatePanel() {
		this.setAlignmentY(Component.TOP_ALIGNMENT);
		// bigGroupPanel.add(this);
		this.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		this.setLayout(new FormLayout(new ColumnSpec[] { ColumnSpec.decode("60px"), FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC, }, new RowSpec[] { FormFactory.DEFAULT_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, }));

		JLabel lblNewLabel = new JLabel("Due Date:");
		this.add(lblNewLabel, "1, 1, right, center");

		dateTimePicker = new DateTimePicker();
		dateTimePicker.getRenderer().setPreferredSize(new Dimension(145, 23));
		dateTimePicker.getRenderer().setMinimumSize(new Dimension(145, 23));
		dateTimePicker.getRenderer().setMaximumSize(new Dimension(145, 23));
		this.add(dateTimePicker, "2,1,left,center");

		JLabel lblQuickDateTime = new JLabel("QuickSet:");
		this.add(lblQuickDateTime, "1, 3, right, center");

		comboBoxQuickDateTime = new JComboBox();
		comboBoxQuickDateTime.setMaximumSize(new Dimension(145, 32767));
		comboBoxQuickDateTime.setPreferredSize(new Dimension(145, 20));
		comboBoxQuickDateTime.setMinimumSize(new Dimension(145, 18));
		comboBoxQuickDateTime.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				applyTimePreset();
			}
		});
		comboBoxQuickDateTime.setModel(new DefaultComboBoxModel(DateTimePreset.values()));
		comboBoxQuickDateTime.setRenderer(new DefaultListCellRenderer() {
			public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
				DateTimePreset time = (DateTimePreset) value;
				return new JLabel(time.label);
			}
		});
		this.add(comboBoxQuickDateTime, "2, 3, left, center");
		// return this;
	}
	
	public void applyTimePreset() {
		DateTime currentDateTime = DateTime.now();
		DateTime modifiedDate = new DateTime();
		// Trace.info(currentDateTime.dayOfWeek().getAsText());

		DateTimePreset preset = (DateTimePreset) comboBoxQuickDateTime.getSelectedItem();

		int addDays = 1;
		if (currentDateTime.dayOfWeek().getAsText().equals("Friday")) {
			addDays = 3;
		}

		int whichDay;
		int whichMonth;
		int whichYear;

		switch (preset) {
			case LIVE:
				modifiedDate = new DateTime(currentDateTime.getYear(), currentDateTime.getMonthOfYear(), currentDateTime.getDayOfMonth(), preset.time.getHourOfDay(), preset.time.getMinuteOfHour());
				break;
			case DAILY:
				modifiedDate = new DateTime(currentDateTime.getYear(), currentDateTime.getMonthOfYear(), currentDateTime.getDayOfMonth(), preset.time.getHourOfDay(), preset.time.getMinuteOfHour());
				break;
			case NEXTLIVE:
				whichDay = currentDateTime.getDayOfMonth() + addDays;
				whichMonth = currentDateTime.getMonthOfYear();
				whichYear = currentDateTime.getYear();
				if (whichDay > 31) {
					whichDay -= 31;
					whichMonth++;
				}
				if (whichMonth > 12) {
					whichMonth -= 12;
					whichYear++;
				}

				modifiedDate = new DateTime(whichYear, whichMonth, whichDay, preset.time.getHourOfDay(), preset.time.getMinuteOfHour());
				break;
			case NEXTDAILY:
				whichDay = currentDateTime.getDayOfMonth() + addDays;
				whichMonth = currentDateTime.getMonthOfYear();
				whichYear = currentDateTime.getYear();
				if (whichDay > 31) {
					whichDay -= 31;
					whichMonth++;
				}
				if (whichMonth > 12) {
					whichMonth -= 12;
					whichYear++;
				}
				modifiedDate = new DateTime(whichYear, whichMonth, whichDay, preset.time.getHourOfDay(), preset.time.getMinuteOfHour());
				break;
		}
		dateTimePicker.setDate(modifiedDate.toDate());
	}
}
